package com.example.backend.service.impl;

import com.example.backend.entity.Business;
import com.example.backend.entity.Customer;
import com.example.backend.entity.Driver;
import com.example.backend.entity.Vehicle;
import com.example.backend.repository.BusinessRepository;
import com.example.backend.repository.CustomerRepository;
import com.example.backend.repository.DriverRepository;
import com.example.backend.repository.VehicleRepository;
import com.example.backend.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessRepository businessRepository;
    private CustomerRepository customerRepository;
    private VehicleRepository vehicleRepository;
    private DriverRepository driverRepository;
    public BusinessServiceImpl(CustomerRepository customerRepository,VehicleRepository vehicleRepository,DriverRepository driverRepository){
        this.customerRepository = customerRepository;
        this.vehicleRepository = vehicleRepository;
        this.driverRepository = driverRepository;
    }
    /*@Override
    public Business save(Business business) {
        Business b=businessRepository.save(business);
        //从数据库中插入数据的情况
        if (b.getCid()!=null){
            Integer cid = b.getCid();
            List<Business> bbb = businessRepository.findByCid(cid);
            Integer count = bbb.size();
            Customer c = customerRepository.findById(cid).get();
            c.setCount(count);
            customerRepository.save(c);
        }
        if (b.getDid()!=null){
            Integer did=b.getDid();
            List<Business> bbb = businessRepository.findByDid(did);
            Integer count = bbb.size();
            Driver d =driverRepository.findById(did).get();
            d.setCount(count);
            driverRepository.save(d);
        }
        return b;
    }*/
    @Override
    public Business save(Business business) {//添加一条类型设备信息
        return businessRepository.save(business);
    }
    @Override
    public void delete(Integer id) {
        Business business = businessRepository.findById(id).get();
        businessRepository.delete(business);
    }
    @Override
    public Business update(Business business) {
        return businessRepository.save(business);
    }
    @Override
    public List<Business> findAll() {
        return businessRepository.findAll();
    }

    @Override
    public List<Business> findById(Integer id) {
        if (!businessRepository.findById(id).isPresent()){
            return null;
        }
        else {
            Business b =businessRepository.findById(id).get();
            return List.of(b);
        }
    }

    //按客户名称检索到的记录是存在与名称相对应的客户编号所在的记录
    @Override
    public List<Business> findByCname(String cname) {
        List<Customer> c = customerRepository.findByName(cname);
        if (customerRepository.findByName(cname).isEmpty()){
            return null;
        }
        Integer id=c.get(0).getId();
        return businessRepository.findByCid(id);
    }

    @Override
    public List<Business> findByVname(String vname) {
        List<Vehicle> v = vehicleRepository.findByName(vname);
        if (vehicleRepository.findByName(vname).isEmpty()){
            return null;
        }
        Integer id=v.get(0).getId();
        return businessRepository.findByVid(id);
    }

    @Override
    public List<Business> findByDname(String dname) {
        List <Driver>  d = driverRepository.findByName(dname);
        if (driverRepository.findByName(dname).isEmpty()) {
            return null;
        }
        Integer id=d.get(0).getId();
        return businessRepository.findByDid(id);
    }

    @Override
    public List<Business> findByMode(String mode) {
        return businessRepository.findByMode(mode);
    }

    @Override
    public List<Business> findByStartDate(String startDate) {
        System.out.println(startDate);
        return businessRepository.findByStartDate(startDate);
    }

    @Override
    public List<Business> findByFinishDate(String finishDate) {
        return businessRepository.findByFinishDate(finishDate);
    }

}
